package com.core.zyyz.amc.modules.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.core.page.TableDataInfo;
import com.core.common.enums.BusinessType;
import com.core.common.utils.StringUtils;
import com.core.zyyz.amc.modules.domain.AmcBusinessCategories;
import com.core.zyyz.amc.modules.domain.AmcCodeManage;
import com.core.zyyz.amc.modules.domain.AmcSystemMainlistD;
import com.core.zyyz.amc.modules.service.AmcBusinessCategoriesService;
import com.core.zyyz.amc.modules.service.AmcCodeManageService;
import com.core.zyyz.amc.modules.service.AmcSystemMainlistDService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 主页配置化功能控制器Controller
 *
 * @author LiuFushun
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/amc/systemMainConfigD")
public class AmcSystemMainlistDController extends BaseController {

    @Autowired
    private AmcSystemMainlistDService amcSystemMainlistDService;
    @Autowired
    private AmcCodeManageService amcCodeManageService;
    @Autowired
    private AmcBusinessCategoriesService amcBusinessCategoriesService;

    /**
     * 查询功能编号管理列表
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcSystemMainlistD paramMap) {
        startPage();
        List<AmcSystemMainlistD> list = amcSystemMainlistDService.queryMainConfigList(paramMap);
        return getDataTable(list);
    }

    /**
     * 主页配置新增
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:add')")
    @Log(title = "主页配置新增", businessType = BusinessType.INSERT)
    @PostMapping("/insertMainConfig")
    @Transactional
    public AjaxResult insertMainConfig(@RequestBody Map<String, Object> paramMap) {
        return toAjax(amcSystemMainlistDService.insertAmcSystemMainListD(paramMap));
    }


    /**
     * 主页配置修改
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:add')")
    @Log(title = "主页配置修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMainConfig")
    @Transactional
    public AjaxResult updateMainConfig(@RequestBody Map<String, Object> paramMap) {
        return toAjax(amcSystemMainlistDService.updateAmcSystemMainListD(paramMap));
    }

    /**
     * 通过主键查询主页配置化信息
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:query')")
    @GetMapping("/query/mainlist")
    public TableDataInfo queryMainListNode (@RequestParam String baseBusinessNode) {
        return getDataTable(amcSystemMainlistDService.queryMainListNode(baseBusinessNode));
    }

    /**
     * 通过主键查询主页配置化排序信息
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:query')")
    @GetMapping("/query/sortlist")
    public TableDataInfo querySortListNode (@RequestParam String baseBusinessNode) {
        return getDataTable(amcSystemMainlistDService.querySortListNode(baseBusinessNode));
    }

    /**
     * 批量删除主页配置数据
     */
    @PreAuthorize("@ss.hasPermi('amc:systemMainConfigD:remove')")
    @Log(title = "功能编号管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseBusinessNodes}")
    @Transactional
    public AjaxResult remove(@PathVariable String[] baseBusinessNodes) {
        return toAjax(amcSystemMainlistDService.deleteAllDetails(baseBusinessNodes));
    }


    /**
     * 查询功能定义列表（下拉框内容）
     *
     *  @param param-configType 用来区分base_business_node查的哪张表
     *  @param param-functionName 页面搜索值
     */
    @GetMapping("/queryFunctionList")
    public TableDataInfo queryFunctionList(AmcCodeManage param ) {
        List<AmcCodeManage> list = amcCodeManageService.queryFunctionList(param);
        return getDataTable(list);
    }

    @PostMapping("/query/categoriespage")
    public TableDataInfo queryBusinessCategoriesListPage(@RequestBody Map<String, Object> param) {
        String multiple = MapUtils.getString(param, "multiple");
        List<AmcBusinessCategories> treeList = amcBusinessCategoriesService.selectAmcBusinessCategoriesList(new AmcBusinessCategories());
        treeList.stream().filter(t -> !StringUtils.isNotEmpty(t.getBizParentId())).forEach(t -> t.setBizParentId("-1"));
        treeList.stream().forEach(t -> {
            if (t.getIsLeaf() == null || t.getIsLeaf().equals("false")) {
                t.setDisabled(true);
            } else {
                t.setDisabled(false);
            }
            t.setChecked(false);
        });
        if (!"false".equals(multiple) && multiple != null) {
            JSONArray selectedData = JSONArray.parseArray(param.get("selectedData") + "");
            if (selectedData != null && selectedData.size() > 0) {
                treeList.stream().filter(t -> !t.getDisabled() && selectedData.contains(t.getBizName())).forEach(t -> t.setChecked(true));
            }
        }
        return getDataTable(treeList);
    }

}
